package com.hangar.eforms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hangar.eforms.common.Result;
import com.hangar.eforms.dto.FormsDataDto;
import com.hangar.eforms.dto.FormsDataQueryDto;
import com.hangar.eforms.dto.PageDto;
import com.hangar.eforms.service.FormsDataService;
import com.hangar.eforms.vo.FormsDataVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 表单数据记录控制层
 *
 * @author hwn
 */
@RestController
@Tag(name = "表单数据记录接口")
@RequestMapping("/forms-data")
@Validated
public class FormsDataController {
    @Resource
    private FormsDataService formsDataService;

    @Operation(summary = "新增电子表单数据")
    @PostMapping
    public Result<FormsDataDto> create(@RequestBody FormsDataDto dto) {
        return Result.success(formsDataService.insert(dto));
    }

    @Operation(summary = "编辑电子表单数据")
    @PutMapping("{id}")
    public Result update(@PathVariable("id") Integer id, @Valid @RequestBody FormsDataDto dto) {
        dto.setId(id);
        formsDataService.update(dto);
        return Result.success();
    }

    @Operation(summary = "电子表单数据查询")
    @GetMapping("list")
    public Result<IPage<FormsDataVo>> list(PageDto pageDto, @Valid FormsDataQueryDto dto) {
        Page<FormsDataVo> page = new Page<>();
        page.setCurrent(pageDto.getPageNumber());
        page.setSize(pageDto.getPageSize());
        return Result.success(formsDataService.list(page, dto));
    }

    @Operation(summary = "加载电子表单数据")
    @GetMapping("load/{id}")
    public Result<FormsDataVo> load(@PathVariable("id") Integer id) {
        return Result.success(formsDataService.loadById(id));
    }

    @Operation(summary = "查看电子表单数据详情-附带杠改记录")
    @GetMapping("view/{id}")
    public Result<FormsDataVo> view(@PathVariable("id") Integer id) {
        return Result.success(formsDataService.view(id));
    }

    @Operation(summary = "导出电子表单数据报告")
    @PostMapping("export/{id}")
    public void exportPdfReport(@PathVariable("id") Integer id, HttpServletResponse response) {
        formsDataService.exportReport(id, response);
    }

}
